package com.engineering.elink.service.impl;

import org.springframework.stereotype.Service;

import com.engineering.elink.domain.AuthUser;
import com.engineering.elink.service.OAuthApiService;
import com.engineering.elink.util.ConfigUtil;
import com.engineering.elink.util.tool.UUIDUtil;

/**
 * 微信第三方登录
 * 第一步：授权，获取code
 * 第二步：通过code,获取accessToken，openid
 * 第三步:通过accessToken、openid获取用户信息
 * @author GunnyZeng
 *
 */
@Service(value="weiXinOAuth")
@SuppressWarnings("unused")
public class WeiXinOAuthApiServiceImpl implements OAuthApiService{

	private String appid =    ConfigUtil.getValue("weixin_appid").trim();
	private String secret =    ConfigUtil.getValue("weixin_secret").trim();
	private String redirect_URI = ConfigUtil.getValue("weixin_redirect_URI").trim();
	private String getUserInfoURL = ConfigUtil.getValue("weixin_userinfpApiUrl").trim();
	private String authorizeURL = ConfigUtil.getValue("weixin_authorizeURL").trim();
	private String accessTokenURL = ConfigUtil.getValue("weixin_access_token_URL").trim();
	private String scope = ConfigUtil.getValue("weixin_scope").trim();

	@Override
	public String authorize() {
		String state = UUIDUtil.getUUID();
        if(scope != null && !scope.equals(""))
        	 return (new StringBuilder()).append(authorizeURL).append("?appid=").append(appid).append("&redirect_uri=").append(redirect_URI).append("&response_type=").append("code").append("&state=").append(state).append("&scope=").append(scope).append("#wechat_redirect").toString();
        else
            return (new StringBuilder()).append(authorizeURL).append("?appid=").append(appid).append("&redirect_uri=").append(redirect_URI).append("&response_type=").append("code").append("&state=").append(state).append("#wechat_redirect").toString();
    
	}

	@Override
	public AuthUser getUser(Object parame) throws Exception {
//		 String code = (String) parame;
//		 AccessToken accessToken = getAccessToken(code);
//
		 AuthUser user =  new AuthUser();
//         if (accessToken.getAccessToken().equals("")) {
//             throw new Exception("授权失败！");
//         } else {
//             String openID = accessToken.getUserUid();
//             //获取用户信息
//             JSONObject json = showUser(accessToken);
//             
//             if (json!=null&&json.keys().hasNext()) {
//                 user.setId(openID);
//                 user.setAvatarLarge(json.getString("headimgurl"));
//                 user.setNickName(json.getString("nickname"));
//                 user.setGender(json.getString("sex"));
//                 user.setCity(json.getString("city"));
//                 user.setProvince(json.getString("province"));
//                 user.setSource("微信");
//                 
//             } else {
//            	 throw new Exception("很抱歉，我们没能正确获取到您的信息! ");
//             }
//         }
         return user;
	}
	
	/**
	 * 获取AccessToken
	 * @param code
	 * @return
	 * @throws Exception 
	 */
//	private AccessToken getAccessToken(String code) throws Exception{
//		return new AccessToken(client.post(
//				accessTokenURL,
//				new PostParameter[] {
//						new PostParameter("appid", appid),
//						new PostParameter("secret",secret),
//						new PostParameter("grant_type", "authorization_code"),
//						new PostParameter("code", code),
//						}, false, null),"openid");
//	}
	/**
	 * 授权后获取用户json数据
	 * @param accessToken
	 * @return
	 * @throws Exception 
	 */
//	 private JSONObject showUser(AccessToken accessToken) throws Exception {
//		        return client.get(getUserInfoURL, new PostParameter[] {
//		            new PostParameter("openid", accessToken.getUserUid()), 
//		            new PostParameter("access_token", accessToken.getAccessToken())
//		        },accessToken.getAccessToken()).asJSONObject();
//	 }


}
